package com.sansi.bleFloodlight.activity;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;

import com.my.httpapi.api.annotation.Layout;
import com.my.httpapi.api.baseView.BaseActivity;
import com.sansi.bleFloodlight.R;
import com.sansi.bleFloodlight.adapter.TimeAdapter;
import com.sansi.bleFloodlight.model.HaveTimeEvent;
import com.sansi.bleFloodlight.model.TimeModel;
import com.sansi.bleFloodlight.utils.RouterPath;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

@Layout(R.layout.layout_activity_time)
@Route(path = RouterPath.timePath)
public class TimeActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private TimeAdapter timeAdapter;

    private List<TimeModel> list = new ArrayList<>();

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        timeAdapter = new TimeAdapter(list);
        recyclerView.setAdapter(timeAdapter);
        for (int i = 0; i < 4; i++) {
            TimeModel timeModel = new TimeModel(0);
            list.add(timeModel);
        }
        timeAdapter.setNewData(list);
        timeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TimeModel timeModel = timeAdapter.getData().get(position);
                if (timeModel.getFieldType() == 0) {
                    RouterPath.gotoCreateTimePath(position);
                }
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void destory() {
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected String showTitle() {
        return "Scheduled Time";
    }

    @Override
    protected boolean statusBarIsBlack() {
        return false;
    }

    @Override
    protected boolean showLeftArrow() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void haveTimeEvent(HaveTimeEvent haveTimeEvent) {
        if (haveTimeEvent.getType() == 1) {
            TimeModel timeModel = list.get(haveTimeEvent.getPosition());
            timeModel.setFieldType(1);
            list.set(haveTimeEvent.getPosition(), timeModel);
            timeAdapter.notifyItemChanged(haveTimeEvent.getPosition());
        } else {

        }

    }
}
